using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class JobSeekers_resumes : System.Web.UI.Page
{
    string connectionString = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            connectionString =
                 WebConfigurationManager.ConnectionStrings[
                   "RecruitVietnam"].ConnectionString;
            if (Session.Contents["jsuserid"] == null)
            {
                Response.Redirect("login.aspx");
            }
            else
            {
                if (!IsPostBack)
                {                
                    ShowResume();
                }
        
            }
        }
        catch(Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }

    protected void btnNew_Click(object sender, EventArgs e)
    {        
        Response.Redirect("resume.aspx");
    }
    
    protected void btnDelete_Click(object sender, EventArgs e)
    {
    try{
        DeleteResume();
        ShowResume();
        }
        catch (Exception ex)
        {
            literalError.Text = ex.Message;
        }
    }
    void ShowResume()
    {
        SqlServerProvider cls = new 
        SqlServerProvider(connectionString);
        cls.CommandText = 
        "select * from vwJSResumes " +
         " where UserID=@UserID";
        cls.ParameterCollection = 
        new string[1]{"@UserID"};
        cls.ValueCollection = new 
        string[1]{Convert.ToString(
        Session.Contents["jsuserid"])};                  
        DataTable dataTable = cls.GetDataTable( );
        GridViewHelper helper = 
        new GridViewHelper();
        helper.FillDataTableToGridView(
        dataTable, GridView1);
        MakeUrlForImage(dataTable.Rows.Count < 5);
        if (dataTable.Rows.Count == 0)
            btnDelete.Enabled = false;
        if (dataTable.Rows.Count >= 5)
            btnNew.Enabled = false;
        else
            btnNew.Enabled = true;
         
    }
    void MakeUrlForImage(bool enableCopy)
    {
        foreach(GridViewRow gridViewRow in GridView1.Rows)
        {
            string resumeID = gridViewRow.Cells[0].Text;
            string resumeTitle = ((HyperLink)
            gridViewRow.Cells[2].Controls[0]).Text; 
            HyperLink  hyperLinkField;
            hyperLinkField = (HyperLink)
             gridViewRow.Cells[6].FindControl("Copy");
            if(enableCopy)
            {                
                hyperLinkField.NavigateUrl =
                "copyresume.aspx?resumeid=" + resumeID
                + "&title=" + resumeTitle;
                hyperLinkField.Text += 
                "Click here to copy resume";
            }
            else
                hyperLinkField.Text += 
                "You can not copy resume";
            hyperLinkField = (HyperLink)
            gridViewRow.Cells[7].FindControl("View");
            hyperLinkField.NavigateUrl =
            "viewresume.aspx?resumeid=" + resumeID ;
            hyperLinkField.Text += 
            "Click here to view Resume";
            
            if(gridViewRow.Cells[0].Text == 
            Session["jsresume"].ToString()) 
             {
                hyperLinkField = (HyperLink)
                gridViewRow.Cells[2].Controls[0];              
                hyperLinkField.Text += 
                " <img src=\"/icons/check.gif\""+
                " border=0 alt='Activate Resume'>";
               
             }
               
        }
    }
    void DeleteResume()
    {
        string resumeID = "";
        foreach (GridViewRow item in GridView1.Rows)
        {
            CheckBox checkBox = 
            (CheckBox)item.Cells[
            0].FindControl("chkResumeID");
            if (checkBox.Checked)
                resumeID += item.Cells[0].Text + ",";
        }
        if (resumeID != "")
        {
            SqlServerProvider cls = 
            new SqlServerProvider(connectionString);
            cls.CommandText = 
            "delete  from tblJSResumes" +
            " where UserID='" + Convert.ToString(
            Session.Contents["jsuserid"])
            + "' and resumeID in('" +
            resumeID.Replace(",", "','") + "')";
            cls.ExecuteNonQuery( );
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    try{
        GridViewRow gridViewRow =GridView1.Rows[e.RowIndex];
        string resumeID = gridViewRow.Cells[0].Text;
        if(resumeID != "")
            if(ActivateResume(resumeID))
            {
                Session.Contents["jsresume"] = resumeID;
                ShowResume();
            }
    }
    catch (Exception ex)
    {
        literalError.Text = ex.Message;
    }
    }
    bool ActivateResume(string resumeID)
    {
        bool update = false;
        try
        {
            SqlServerProvider cls = new 
            SqlServerProvider(connectionString);
            cls.CommandText = "SPActivateResume";
            cls.CommandType = CommandType.StoredProcedure;
            cls.ParameterCollection = new 
            string[2] {"@UserID", "@ResumeID"}; 
            cls.ValueCollection = new 
            string[2]{Convert.ToString(
            Session.Contents["jsuserid"]), resumeID};            
            update= (cls.ExecuteNonQuery()>0);
        }
        catch(Exception ex)
        {
            literalError.Text = ex.Message;
        }
        return update;
    }
}
